package com.yilin.medical.activity;

import java.util.ArrayList;
import java.util.List;

import com.yilin.medical.R;
import com.yilin.medical.adapter.KeChengDetailsCommentAdapter;
import com.yilin.medical.model.KeChengDetailsCommentBean;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

public class KeChengDetailsActivity extends BaseActivity {
	private Context context;
	private ScrollView mScrollView;
	private ListView mListViewComment;
	private KeChengDetailsCommentAdapter mAdapter;
	private List<KeChengDetailsCommentBean> mList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentViewWithTitle(R.layout.activity_kechengdetails);
		context = this;
		initView();
		registener();
		initData();
	}

	private void initView() {
		mTitle.getTitle().setText("课程详情");
		mScrollView = (ScrollView) findViewById(R.id.sv);
		mListViewComment = (ListView) findViewById(R.id.lv_comment);
	}

	private void registener() {
	}

	private void initData() {
		mList = new ArrayList<KeChengDetailsCommentBean>();
		for (int i = 0; i < 5; i++) {
			mList.add(new KeChengDetailsCommentBean());
		}
		mAdapter = new KeChengDetailsCommentAdapter(context, mList);
		mListViewComment.setAdapter(mAdapter);
		setListViewHeightBasedOnChildren(mListViewComment);
		mScrollView.post(new Runnable() {
			
			@Override
			public void run() {
				mScrollView.smoothScrollTo(0, 0);
				
			}
		});
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		}
	}
	
	public void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}

		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		params.height += 5;
		listView.setLayoutParams(params);
	}

}
